package Simulation;

import java.util.ArrayList;

import Config.Config;
import People.People;
import People.Status;

/**
 *  Changes status of People after contagious
 *  
 * @author Amir BEN SLIMANE
 */
public class SwitchStatus {
	private Map map;

	/**
	 * Builder's SwitchStatus
	 * 
	 * @param map
	 */
	public SwitchStatus(Map map) {
		this.setMap(map);
	}

	/**
	 * Checks all People and changes status if need
	 */
	public void checkToSwitch() {
		ArrayList<Status> list;
		People people;
		for (int x = 0; x < Config.MAP_HEIGTH; x++) {
			for (int y = 0; y < Config.MAP_LENGTH; y++) {
				people = this.map.getMapSimulation()[x][y];
				if (people.getStatus() != Status.Healthy
						|| people.getStatus() != Status.Empty) {
					list = people.getHealthRecord();
					switch (people.getStatus()) {
					case Sick:
						if (list.size() >= 2
								&& list.get(list.size() - 1) == Status.Sick
								&& list.get(list.size() - 2) == Status.Sick) {
							people.setStatus(Status.Contagious);
						}
						break;
					case Contagious:
						if (list.size() >= 3
								&& list.get(list.size() - 1) == Status.Contagious
								&& list.get(list.size() - 2) == Status.Contagious
								&& list.get(list.size() - 3) == Status.Contagious) {
							people.setStatus(Status.Recovering);
						}
						break;
					case Recovering:
						if ((int) (Math.random() * 101) < Config.DEATH_RATE) {
							people.setStatus(Status.Dead);
						} else if (list.size() >= 3
								&& list.get(list.size() - 1) == Status.Recovering
								&& list.get(list.size() - 2) == Status.Recovering
								&& list.get(list.size() - 3) == Status.Recovering) {
							people.setStatus(Status.Healthy);
						}
						break;
					default:
						break;
					}
				}
				this.map.getMapSimulation()[x][y] = people;
			}
		}
	}
	
	/**
	 * @return the map
	 */
	public Map getMap() {
		return map;
	}

	/**
	 * @param map
	 *            the map to set
	 */
	public void setMap(Map map) {
		this.map = map;
	}
}
